<template>
  <div>

    <div class="detail_title">
      <h4 v-text="pageName"></h4>
      <a class="close" @click="close">╳</a>
    </div>

    <div class="device_div"></div>

    <div class="quote">
      <div class="quoteTop">
        <h4 class="panel-heading">团队信息</h4>

        <div class="query">
          <Row>

            <Col span="12" class="input_info" v-if="isShowPartnerCom">
            <label>营业部</label>

            <Select size="small" style="width: 50%" v-model="inputInfo.partnerId" >
              <Option v-for="item in partnerComList" :value="item.id + ''">{{item.partnerName}}</Option>
            </Select>
            </Col>

            <Col span="12" class="input_info">
            <label>团队名称</label>
            <Input size="small" style="width: 50%" v-model="inputInfo.teamName"> </Input>
            </Col>

            <Col span="12" class="input_info" v-if="isShowTeamLeader">
            <label>团队长</label>
            <!--<Select size="small" style="width: 50%" v-model="inputInfo.teamLeaderId" filterable>-->
              <!--<Option v-for="(item, index) in teamMemeberList" v-bind:value="item.id">{{item.name}}-{{item.mobile}}</Option>-->
            <!--</Select>-->
              <Input size="small" style="width: 50%" v-model="inputInfo.teamLeaderName" @on-focus="showAgentModul"> </Input>
            </Col>

            <Col span="12" class="input_info">
            <label>状态</label>
            <Select size="small" style="width: 50%" v-model="inputInfo.state">
              <Option v-for="item in state" :value="item.code">{{item.name}}</Option>
            </Select>
            </Col>

          </Row>
        </div>

      </div>
    </div>

    <div class="bottom_btn_div">

      <Button size="small" @click="saveInfo" type="primary">提交</Button>
      <Button size="small" v-if="teamId ==='-1'" @click="resetInfo" type="error">重 置</Button>
      <Button size="small" @click="close">关 闭</Button>

    </div>
    <agent-select
        v-if="isShowModal"
        v-model="isShowModal"
        :queryObj="agentQueryObj"
        @selectedAgent="selectedAgent">
    </agent-select>
  </div>
</template>

<script type="text/javascript">
  import api from '../../utils/api.js';
  //  import commonCode from '../../assets/json/common_code.json';
  import {queryROLE, queryPartnerId, queryPartnerName} from '../../utils/common.js';
  import reqDic from '../../utils/requestDictionary.js';
  import AgentSelect from "aoyun-business-components/src/components/AgentSelect/AgentSelect";

  export default {
      components: {AgentSelect},
      data: function () {
      return {

        state: reqDic.QueryDictionary("COMMONSTATE"),

        inputInfo: {
          teamName: "",
          state: ""
//          partnerId: JSON.parse(localStorage.Manage).USER_PARTNER_PRIVILEGE.id,
        },

        teamId: '',
        partnerId: '',

        pageName: '',

        teamMemeberList: [],
        //营业部list
        partnerComList: [],

        //我的角色
        myRole: '',
        isShowTeamLeader: false,
        isShowPartnerCom: false,
        isCanPartnerCom: false,
          isShowModal:false,
          agentQueryObj:{}

      }
    },
    created: function () {

      this.teamId = this.$route.params.id;
      this.partnerId = this.$route.params.partnerId;

      this.permissionControl();

      var self = this;

      api.getPartnerComList().then(res => {
        self.partnerComList = res.body.data;
        if (self.teamId !== '-1') {
          self.pageName = '修改团队';

          // self.getAgentList();
            api.getTeamDetailInfo(self.teamId).then(res => {
                  self.inputInfo = res.body.data;
            });
        } else {

          self.pageName = '新增团队';

        }
      });



    },
    methods: {
      //权限控制的方法
      permissionControl: function () {
        this.myRole = queryROLE();

        if (this.teamId == -1 || this.myRole == 3) {
          this.isShowTeamLeader = false;
        } else {
          this.isShowTeamLeader = true;
        }

        if (this.myRole == 1) {
          this.isShowPartnerCom = true;
        } else {
          this.isShowPartnerCom = false;
        }

        this.isCanPartnerCom = this.myRole != 1 || this.teamId != -1;
        console.log(this.isCanPartnerCom);

      },

      saveInfo: function () {

        var self = this;

        if (this.teamId === '-1') {
            if(queryROLE()=='2'){
                this.inputInfo.partnerId = this.partnerId
            }

          api.saveTeamInfo(this.inputInfo.teamName, this.inputInfo.state, this.inputInfo.partnerId).then((res) => {

            if (res.body.state === "1") {
              self.$Message.success(res.body.message.info);
            } else {
              self.$Message.error(res.body.message.info);
            }

          })

        } else {

          this.teamMemeberList.forEach(function (item) {
            if (item.id === self.inputInfo.teamLeaderId) {
                console.log(1212,item)
              self.inputInfo.teamLeaderName = item.name + '-' + item.mobile;
            }
          });

          api.updateTeamDetailInfo(this.inputInfo).then((res) => {

            if (res.body.state === "1") {
              self.$Message.success(res.body.message.info);
            } else {
              self.$Message.error(res.body.message.info);

            }
          })

        }
      },

      // getAgentList: function () {
      //   var self = this;
      //   api.getAgentList(this.partnerId, this.teamId).then(res => {
      //     self.teamMemeberList = res.body.data;
      //
      //     api.getTeamDetailInfo(self.teamId).then(res => {
      //       self.inputInfo = res.body.data;
      //     });
      //
      //   });
      // },

      resetInfo: function () {

        this.inputInfo = {
          teamName: "",
          state: "",
//          partnerId: JSON.parse(localStorage.Manage).USER_PARTNER_PRIVILEGE.id,
        };

      },

      close: function () {
        this.$router.go(-1);
      },
        showAgentModul(){
            this.isShowModal = true
            this.agentQueryObj = {
                partnerId:this.partnerId,
                teamId:this.teamId
            }
        },
        selectedAgent(agentInfo){
          console.log(agentInfo)
            this.inputInfo.teamLeaderName = agentInfo.selectedInfo.item.name + '-' + agentInfo.selectedInfo.item.mobile
            this.inputInfo.teamLeaderId = agentInfo.selectedInfo.item.id
        }

    },
  }
</script>
